package com.konne.nightmare.FastPublicOpinion.widget;

import static com.konne.nightmare.FastPublicOpinion.widget.QJItemTitleView.Style.check;
import static com.konne.nightmare.FastPublicOpinion.widget.QJItemTitleView.Style.custom;
import static com.konne.nightmare.FastPublicOpinion.widget.QJItemTitleView.Style.none;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.konne.nightmare.FastPublicOpinion.R;

/**
 * Create by ws.
 * on Data:2022/2/10  Time:下午 4:49.
 * description: 条目控件 .
 */
public class QJItemTitleView extends FrameLayout {
    private Style style;
    private boolean isShowLine;
    private ViewGroup content;
    private QJSwitch qjSwitch;
    private boolean checked;
    private QJCheckBox qjCheckBox;
    private OnCheckedChangeListener onCheckedChangeListener;
    public QJItemTitleView(@NonNull Context context) {
        this(context, null);
    }

    public QJItemTitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QJItemTitleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.QJItemTitleView, defStyleAttr, 0);
        int s = a.getInt(R.styleable.QJItemTitleView_qjit_style, 0);
        style = Style.getStyleByType(s);
        String title = a.getString(R.styleable.QJItemTitleView_qjit_title);
        isShowLine = a.getBoolean(R.styleable.QJItemTitleView_qjit_line, false);
        checked = a.getBoolean(R.styleable.QJItemTitleView_qjit_checked, false);
        int txtSize = a.getInt(R.styleable.QJItemTitleView_qjit_titleSize, 14);
        int txtColor = a.getColor(R.styleable.QJItemTitleView_qjit_titleColor, 0xff1C2B33);
        a.recycle();
        ViewGroup view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.view_item_title, null);
        addView(view, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        TextView tv_title = (TextView) view.getChildAt(0);
        tv_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, txtSize);
        tv_title.setTextColor(txtColor);
        if (!StringUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
        content = (ViewGroup) view.getChildAt(1);
        setStyle(style);
    }

    public ViewGroup getContentView() {
        return content;
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    public void setTitleColor(int color) {
        getTitleTextView().setTextColor(color);
    }

    public void setTitleSize(int size) {
        getTitleTextView().setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

    public void setTitle(CharSequence txt) {
        getTitleTextView().setText(txt);
    }

    private TextView getTitleTextView() {
        return (TextView) ((ViewGroup) getChildAt(0)).getChildAt(0);
    }

    public void setShowLine(boolean showLine) {
        isShowLine = showLine;
    }

    public void setStyle(Style style) {
        this.style = style;
        initView();
    }

    public void showLineView(boolean isShow) {
        if(getChildCount()>1){
            getChildAt(1).setVisibility(isShow?VISIBLE:GONE);
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (style == custom && getChildCount() > 1) {
            View view = getChildAt(1);
            removeView(view);
            content.addView(view);
            if (isShowLine) {
                View v = new View(getContext());
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(1));
                if (style == Style._switch) {
                    params.rightMargin = SizeUtils.dp2px(15);
                }
                v.setLayoutParams(params);
                params.gravity = Gravity.BOTTOM;
                v.setBackgroundColor(0x4fD7D5E0);
                addView(v);
            }
        }


    }

    private void initView() {
        switch (style) {
            case custom:
            case none:
                content.removeAllViews();
                break;
            case right:
                content.addView(getRightImg());
                break;
            case _switch:
                qjSwitch = getSwitchView();
                qjSwitch.setChecked(checked);
                content.addView(qjSwitch);
                break;
            case check:
                qjCheckBox = getCheckBox();
                qjCheckBox.setChecked(checked);
                content.addView(qjCheckBox);
                break;
        }
        if (style!=custom && isShowLine) {
            View view = new View(getContext());
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(1));
            if (style == Style._switch) {
                params.rightMargin = SizeUtils.dp2px(15);
            }
            view.setLayoutParams(params);
            params.gravity = Gravity.BOTTOM;
            view.setBackgroundColor(0x4fD7D5E0);
            addView(view);
        }
    }

    public enum Style {
        right(1), none(0), _switch(2), custom(3), check(4);
        public int type;

        Style(int i) {
            this.type = i;
        }

        public static Style getStyleByType(int t) {
            switch (t) {
                case 1:
                    return right;
                case 2:
                    return _switch;
                case 3:
                    return custom;
                case 4:
                    return check;
                default:
                    return none;
            }
        }
    }

    public QJSwitch getQJSwitch() {
        return qjSwitch;
    }

    public QJCheckBox getQjCheckBox() {
        return qjCheckBox;
    }

    public boolean isChecked() {
        return checked;
    }


    private ImageView getRightImg() {
        ImageView imageView = new ImageView(getContext());
        imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        imageView.setImageResource(R.drawable.arrow_right_small);
        return imageView;
    }

    private QJCheckBox getCheckBox() {
        QJCheckBox imageView = new QJCheckBox(getContext());
        imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        imageView.setOnCheckedChangeListener(new QJCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(QJCheckBox var1, boolean var2) {
                checked = var2;
                if(onCheckedChangeListener!=null){
                    onCheckedChangeListener.onCheckedChanged(QJItemTitleView.this,style,checked);
                }
            }
        });
        return imageView;
    }

    private QJSwitch getSwitchView() {
        QJSwitch imageView = new QJSwitch(getContext());
        imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        imageView.setOnCheckedChangeListener(new QJSwitch.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(QJSwitch var1, boolean var2) {
                checked = var2;
                if(onCheckedChangeListener!=null){
                    onCheckedChangeListener.onCheckedChanged(QJItemTitleView.this,style,checked);
                }
            }
        });
        return imageView;
    }
    public interface OnCheckedChangeListener{
        void onCheckedChanged(QJItemTitleView titleView,Style style,boolean isChecked);
    }
}
